<template>
  <view class="register-container">
    <!-- 设计说明 -->
    <view class="design-notes">
      <text class="note-title">设计说明</text>
      <text class="note-item">1. 多邻国风格的注册界面</text>
      <text class="note-item">2. 响应式布局适配移动设备</text>
      <text class="note-item">3. 表单验证功能</text>
      <text class="note-item">4. 密码强度提示</text>
      <text class="note-item">5. 注册按钮状态反馈</text>
    </view>
    
    <!-- 注册表单 -->
    <view class="register-form">
      <view class="logo-container">
        <image class="logo" src="/static/logo.png"></image>
      </view>
      <view class="form-title">创建账号</view>
      
      <view class="input-group">
        <view class="input-item">
          <text class="input-label">用户名</text>
          <view class="input-wrapper">
            <input 
              class="input" 
              type="text" 
              v-model="username" 
              placeholder="请输入用户名" 
              @blur="validateUsername"
            />
            <text v-if="username && !errors.username" class="input-icon check">✓</text>
          </view>
          <text v-if="errors.username" class="error-tip">{{ errors.username }}</text>
        </view>
        
        <view class="input-item">
          <text class="input-label">邮箱</text>
          <view class="input-wrapper">
            <input 
              class="input" 
              type="email" 
              v-model="email" 
              placeholder="请输入邮箱" 
              @blur="validateEmail"
            />
            <text v-if="email && !errors.email" class="input-icon check">✓</text>
          </view>
          <text v-if="errors.email" class="error-tip">{{ errors.email }}</text>
        </view>
        
        <view class="input-item">
          <text class="input-label">密码</text>
          <view class="input-wrapper">
            <input 
              class="input" 
              :type="showPassword ? 'text' : 'password'" 
              v-model="password" 
              placeholder="请输入密码" 
              @blur="validatePassword"
              @input="checkPasswordStrength"
            />
            <text 
              class="password-toggle" 
              @click="showPassword = !showPassword"
            >{{ showPassword ? '隐藏' : '显示' }}</text>
          </view>
          <text v-if="errors.password" class="error-tip">{{ errors.password }}</text>
          
          <!-- 密码强度指示器 -->
          <view v-if="password" class="password-strength">
            <text class="strength-label">密码强度：</text>
            <view class="strength-bars">
              <view 
                class="strength-bar" 
                :class="{
                  'weak': passwordStrength >= 1,
                  'medium': passwordStrength >= 2,
                  'strong': passwordStrength >= 3
                }"
              ></view>
              <view 
                class="strength-bar" 
                :class="{
                  'medium': passwordStrength >= 2,
                  'strong': passwordStrength >= 3
                }"
              ></view>
              <view 
                class="strength-bar" 
                :class="{
                  'strong': passwordStrength >= 3
                }"
              ></view>
            </view>
            <text class="strength-text">{{ passwordStrengthText }}</text>
          </view>
        </view>
        
        <view class="input-item">
          <text class="input-label">确认密码</text>
          <view class="input-wrapper">
            <input 
              class="input" 
              :type="showConfirmPassword ? 'text' : 'password'" 
              v-model="confirmPassword" 
              placeholder="请再次输入密码" 
              @blur="validateConfirmPassword"
            />
            <text 
              class="password-toggle" 
              @click="showConfirmPassword = !showConfirmPassword"
            >{{ showConfirmPassword ? '隐藏' : '显示' }}</text>
          </view>
          <text v-if="errors.confirmPassword" class="error-tip">{{ errors.confirmPassword }}</text>
        </view>
      </view>
      
      <view class="agreement">
        <label class="agreement-label">
          <checkbox 
            :checked="agreeTerms" 
            @click="agreeTerms = !agreeTerms"
            color="#58CC02"
            style="transform:scale(0.8)"
          />
          <text>我已阅读并同意</text>
          <text class="link">服务条款</text>
          <text>和</text>
          <text class="link">隐私政策</text>
        </label>
      </view>
      
      <button 
        class="register-btn" 
        :disabled="!isFormValid || isLoading" 
        :class="{'loading': isLoading}"
        @click="handleRegister"
      >
        {{ isLoading ? '注册中...' : '注册' }}
      </button>
      
      <view class="login-link">
        <text>已有账号？</text>
        <text class="link" @click="goToLogin">立即登录</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      email: '',
      password: '',
      confirmPassword: '',
      showPassword: false,
      showConfirmPassword: false,
      agreeTerms: false,
      isLoading: false,
      passwordStrength: 0,
      errors: {
        username: '',
        email: '',
        password: '',
        confirmPassword: ''
      }
    }
  },
  computed: {
    isFormValid() {
      return this.username && this.email && this.password && this.confirmPassword && 
             this.agreeTerms && !this.errors.username && !this.errors.email && 
             !this.errors.password && !this.errors.confirmPassword
    },
    passwordStrengthText() {
      switch(this.passwordStrength) {
        case 0: return '太短';
        case 1: return '弱';
        case 2: return '中等';
        case 3: return '强';
        default: return '';
      }
    }
  },
  methods: {
    validateUsername() {
      if (!this.username) {
        this.errors.username = '请输入用户名'
      } else if (this.username.length < 4) {
        this.errors.username = '用户名长度不能少于4位'
      } else {
        this.errors.username = ''
      }
    },
    validateEmail() {
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
      if (!this.email) {
        this.errors.email = '请输入邮箱'
      } else if (!emailRegex.test(this.email)) {
        this.errors.email = '请输入有效的邮箱地址'
      } else {
        this.errors.email = ''
      }
    },
    validatePassword() {
      if (!this.password) {
        this.errors.password = '请输入密码'
      } else if (this.password.length < 6) {
        this.errors.password = '密码长度不能少于6位'
      } else {
        this.errors.password = ''
      }
      
      if (this.confirmPassword) {
        this.validateConfirmPassword()
      }
    },
    validateConfirmPassword() {
      if (!this.confirmPassword) {
        this.errors.confirmPassword = '请确认密码'
      } else if (this.confirmPassword !== this.password) {
        this.errors.confirmPassword = '两次输入的密码不一致'
      } else {
        this.errors.confirmPassword = ''
      }
    },
    checkPasswordStrength() {
      if (!this.password || this.password.length < 6) {
        this.passwordStrength = 0
        return
      }
      
      let strength = 0
      
      // 检查长度
      if (this.password.length >= 8) strength += 1
      
      // 检查复杂性
      if (/[A-Z]/.test(this.password) && /[a-z]/.test(this.password)) strength += 1
      if (/[0-9]/.test(this.password)) strength += 0.5
      if (/[^A-Za-z0-9]/.test(this.password)) strength += 0.5
      
      this.passwordStrength = Math.min(Math.floor(strength), 3)
    },
    handleRegister() {
      this.validateUsername()
      this.validateEmail()
      this.validatePassword()
      this.validateConfirmPassword()
      
      if (this.isFormValid) {
        this.isLoading = true
        
        // 模拟注册请求
        setTimeout(() => {
          this.isLoading = false
          uni.showToast({
            title: '注册成功',
            icon: 'success'
          })
          // 注册成功后跳转到分析页面
          setTimeout(() => {
            uni.navigateTo({
              url: '/pages/analysis/analysis'
            })
          }, 1000)
        }, 1500)
      }
    },
    goToLogin() {
      uni.navigateTo({
        url: '/pages/login/login'
      })
    }
  }
}
</script>

<style>
.register-container {
  display: flex;
  min-height: 100vh;
  background-color: #fff;
  position: relative;
}

/* 设计说明样式 */
.design-notes {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: rgba(255, 255, 255, 0.9);
  padding: 10px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 10;
  max-width: 200px;
}

.note-title {
  font-weight: bold;
  font-size: 14px;
  margin-bottom: 5px;
  display: block;
  color: #333;
}

.note-item {
  font-size: 12px;
  color: #666;
  display: block;
  margin-bottom: 3px;
}

/* 注册表单样式 */
.register-form {
  width: 100%;
  padding: 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.logo-container {
  background-color: #58CC02;
  width: 100px;
  height: 100px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 30px 0 20px;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.2);
}

.logo {
  width: 70px;
  height: 70px;
}

.form-title {
  font-size: 28px;
  font-weight: bold;
  color: #4b4b4b;
  margin-bottom: 30px;
}

.input-group {
  width: 100%;
  margin-bottom: 20px;
}

.input-item {
  margin-bottom: 20px;
  position: relative;
}

.input-label {
  font-size: 15px;
  color: #4b4b4b;
  margin-bottom: 8px;
  display: block;
  font-weight: 500;
}

.input-wrapper {
  position: relative;
  width: 100%;
}

.input {
  width: 100%;
  height: 50px;
  border: 2px solid #e5e5e5;
  border-radius: 12px;
  padding: 0 15px;
  font-size: 16px;
  background-color: #f7f7f7;
  transition: all 0.3s ease;
}

.input:focus {
  border-color: #58CC02;
  background-color: #fff;
  outline: none;
  box-shadow: 0 0 0 4px rgba(88, 204, 2, 0.15);
}

.input-icon.check {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #58CC02;
  font-size: 18px;
  font-weight: bold;
}

.password-toggle {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 14px;
  color: #58CC02;
  font-weight: bold;
}

.error-tip {
  color: #ff4b4b;
  font-size: 13px;
  margin-top: 5px;
  display: block;
  font-weight: 500;
}

/* 密码强度指示器 */
.password-strength {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
}

.strength-label {
  font-size: 13px;
  color: #4b4b4b;
  margin-bottom: 5px;
}

.strength-bars {
  display: flex;
  gap: 5px;
  margin-bottom: 5px;
}

.strength-bar {
  height: 6px;
  flex: 1;
  background-color: #e5e5e5;
  border-radius: 3px;
}

.strength-bar.weak {
  background-color: #ff4b4b;
}

.strength-bar.medium {
  background-color: #ffb020;
}

.strength-bar.strong {
  background-color: #58CC02;
}

.strength-text {
  font-size: 13px;
  color: #4b4b4b;
}

.agreement {
  width: 100%;
  margin-bottom: 25px;
}

.agreement-label {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  font-size: 15px;
  color: #4b4b4b;
}

.register-btn {
  width: 100%;
  height: 55px;
  background-color: #58CC02;
  color: white;
  border: none;
  border-radius: 16px;
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 0 #58a700;
  transition: all 0.2s ease;
}

.register-btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 #58a700;
}

.register-btn.loading {
  opacity: 0.9;
}

.register-btn:disabled {
  background-color: #afafaf;
  box-shadow: 0 4px 0 #8f8f8f;
}

.login-link {
  font-size: 16px;
  color: #4b4b4b;
  margin-top: 10px;
}

.link {
  color: #58CC02;
  margin-left: 5px;
  font-weight: bold;
  text-decoration: underline;
}
</style>